/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;


/**
 *
 * @author Carlos
 */
public class PonNotas extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        /*SocialFP red= new SocialFP();
        try {
                    red.recuperaUjaenSocial();
                }catch (Exception e){
                 notificarError (request, response, "Error", "No se puede recuperar el estado de la red");
                }*/
        
        SocialFP red=  (SocialFP) request.getSession ().getAttribute ("red");
        if (red == null) {
            notificarError (request, response, "Error", "Acceso ilegal o sesión caducada");
            return;
        }
        String email=  (String) request.getSession ().getAttribute ("email");
         String clave=  (String) request.getSession ().getAttribute ("clave");
        Usuario u = red.loginUsuario(email, clave);
         if (u == null) {
            notificarError (request, response, "Error", "Acceso ilegal o sesión caducada");
            return;
        }
         if (!"admin".equals(u.leeEmail())){
             notificarError (request, response, "Error", "Acceso ilegal o sesión caducada");
            return;
         }
         
         String modulo= (String) request.getParameter ("Modulo");
         
         if (!"redes".equals(modulo) && !"hardware".equals(modulo)){
             notificarError (request, response, "Error", "No seleccionó ningún modulo");
          // RequestDispatcher dispatcher = request.getRequestDispatcher(response.encodeURL("mostrarUsuario"));
           //dispatcher.forward (request, response);
           return;
         }
         
         response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
         // Imprimir cabecera
        out.println ("<body alink=\"#000099\" bgcolor=\"#6d9fff\" link=\"#000099\" text=\"#000000\" vlink=\"#990099\">");
        out.println ("<table cellspacing=\"5\" bgcolor=\"#2c90c9\" width = \"600\">");
        out.println ("<tr><td><img src=\"imagenes/cabecera.jpg\" align=\"bottom\" height=\"100\" width=\"80\"></td>");
        out.println ("<td align=\"left\" td valign=\"middle\"><font color=\"#f9f9f9\"><big><big><big><b>SocialfP</b></font></big></big></big></td></tr>");
        out.println ("</table>");
        
         // Imprimimos notas de todos los alumnos y del modulo en cuestion
       if ("redes".equals(modulo)){

        ArrayList lista;
        Usuario temp;
        lista= new ArrayList();
        lista=(ArrayList)red.todosUsuarios();
        Iterator itera = lista.listIterator();
      	while(itera.hasNext()){
           
            temp=(Usuario) itera.next();
            if (temp.leeEmail().equals(u.leeEmail())){
                
            }else{
        out.println ("<label for=\"Nombre Alumno\" accesskey=\"S\">Nombre Alumno: </label>");
        out.println ("<input type=\"text\" disabled name=\"nombre\" size=20 value=\"" + temp.leeNombre()+ "\"><br>");
        out.println ("<label for=\"Email Alumno\" accesskey=\"S\">Email Alumno: </label>");
        out.println ("<input type=\"text\" disabled name=\"email\" size=30 value=\"" + temp.leeEmail()+ "\"><br>");
        out.println ("<label for=\"Nota redes T1\" accesskey=\"S\">Nota redes T1: </label>");
        out.println ("<input type=\"text\" name=\"redesT1\" size=20 value=\"" + temp.leeNotas().leeNotaredesT1() + "\"><br>");
        out.println ("<label for=\"Nota redes T1\" accesskey=\"S\">Nota redes T2: </label>");
        out.println ("<input type=\"text\" name=\"redesT1\" size=20 value=\"" + temp.leeNotas().leeNotaredesT2() + "\"><br>");
        out.println ("<label for=\"Nota redes T1\" accesskey=\"S\">Nota redes T3: </label>");
        out.println ("<input type=\"text\" name=\"redesT1\" size=20 value=\"" + temp.leeNotas().leeNotaredesT3() + "\"><br>"); 
            }
        }
        
       }else
        if ("hardware".equals(modulo)){

        ArrayList lista;
        Usuario temp;
        lista= new ArrayList();
        lista=(ArrayList)red.todosUsuarios();
        Iterator itera = lista.listIterator();
      	while(itera.hasNext()){
           
            temp=(Usuario) itera.next();
            if (temp.leeEmail().equals(u.leeEmail())){
                
            }else{
        out.println ("<label for=\"Nombre Alumno\" accesskey=\"S\">Nombre Alumno: </label>");
        out.println ("<input type=\"text\" disabled name=\"nombre\" size=20 value=\"" + temp.leeNombre()+ "\"><br>");
        out.println ("<label for=\"Email Alumno\" accesskey=\"S\">Email Alumno: </label>");
        out.println ("<input type=\"text\" disabled name=\"email\" size=30 value=\"" + temp.leeEmail()+ "\"><br>");
        out.println ("<label for=\"Nota redes T1\" accesskey=\"S\">Nota hardware T1: </label>");
        out.println ("<input type=\"text\" name=\"redesT1\" size=20 value=\"" + temp.leeNotas().leeNotahardT1() + "\"><br>");
        out.println ("<label for=\"Nota redes T1\" accesskey=\"S\">Nota hardware T2: </label>");
        out.println ("<input type=\"text\" name=\"redesT1\" size=20 value=\"" + temp.leeNotas().leeNotahardT2() + "\"><br>");
        out.println ("<label for=\"Nota redes T1\" accesskey=\"S\">Nota hardware T3: </label>");
        out.println ("<input type=\"text\" name=\"redesT1\" size=20 value=\"" + temp.leeNotas().leeNotahardT3() + "\"><br>"); 
            }
        }
       }
        
        out.println ("<form action=\"" + response.encodeURL ("mostrarUsuario") + "\" method=\"post\">");
        out.println ("<input type=\"submit\" name=\"Volver\" value=\"Volver\" accessKey=\"V\"><br>");                             
        out.println ("</form>");   
             
         
         
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    protected void notificarError (HttpServletRequest request, HttpServletResponse response, String cabecera, String descripcion) 
        throws ServletException, IOException
    {
        RequestDispatcher dispatcher = request.getRequestDispatcher("error.jsp?cabecera=" + cabecera 
                + "&descripcion=" + descripcion + "&continuar=mostrarUsuario");
        dispatcher.forward (request, response);    
    }   
     protected void notificarExito (HttpServletRequest request, HttpServletResponse response, String cabecera, String descripcion) 
        throws ServletException, IOException
    {
        RequestDispatcher dispatcher = request.getRequestDispatcher("realizado.jsp?cabecera=" + cabecera 
                + "&descripcion=" + descripcion + "&continuar=mostrarUsuario");
        dispatcher.forward (request, response);    
    }
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}